package cn.hh.ymcc.controller;

import cn.hh.ymcc.doc.CourseDoc;
import cn.hh.ymcc.dto.CourseSearchDto;
import cn.hh.ymcc.result.JSONResult;
import cn.hh.ymcc.result.PageList;
import cn.hh.ymcc.service.ICourseEsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 *  课程Es控制层
 */
@RestController
public class CourseEsController {

    @Autowired
    private ICourseEsService courseEsService;


    /**
     *  保存课程到Es
     * @param doc
     * @return
     */
    @PostMapping("/saveCourse")
    @PreAuthorize("hasAuthority('es:saveCourse')") // 方法授权
    public JSONResult saveCourse(@RequestBody CourseDoc doc){
        courseEsService.saveCourse(doc);
        return JSONResult.success();

    }

    /**
     * 删除Es中的课程
     * @param courseId
     * @return
     */
    @DeleteMapping("/deleteByIdCourse/{courseId}")
    public JSONResult deleteByIdCourse(@PathVariable Long courseId){
        courseEsService.deleteByIdCourse(courseId);
        return JSONResult.success();
    }

    /**
     *  课程列表查询
     * @param dto
     * @return
     */
    @PostMapping("/course")
    public JSONResult searchCourse(@RequestBody CourseSearchDto dto){
        PageList<CourseDoc> courseDocs = courseEsService.searchCourse(dto);
        return JSONResult.success(courseDocs);

    }
}
